package com.boot.main.service.impl;

import com.boot.main.entity.CcomUser;
import com.boot.main.entity.ScomCategory;
import com.boot.main.entity.ScomProduct;
import com.boot.main.entity.ScomShop;
import com.boot.main.mapper.CcomUserMapper;
import com.boot.main.mapper.ScomCategoryMapper;
import com.boot.main.mapper.ScomProductMapper;
import com.boot.main.mapper.ScomShopMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IProductService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ProductService implements IProductService {
    @Resource
    private ScomProductMapper productMapper;

    @Resource
    private CcomUserMapper userMapper;

    @Resource
    private ScomShopMapper shopMapper;

    @Resource
    private ScomCategoryMapper categoryMapper;

    public Long getUserId(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals =  subject.getPrincipals();
        String username = (String) principals.getPrimaryPrincipal();
        CcomUser user = userMapper.getUserByUserName(username);
        Long id = user.getId();
        return id;
    }

    @Override
    public ResponseData getProducts(Integer page, Integer limit, String name, Long cid, Long shop_id){
        List<ScomProduct> items = productMapper.getProducts((page-1)*limit, limit, name, cid, shop_id);
        Integer count = productMapper.getTotalCount();
        for (ScomProduct item:items){
            ScomCategory category = categoryMapper.getCategoryById(item.getCid());
            ScomShop shop = shopMapper.getShopById(item.getShop_id());
            item.setCid_name(category.getName());
            item.setShop_name(shop.getName());
        }
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取产品成功", map);
    }

    @Override
    public ResponseData getProductByNum(String pro_number){
        ScomProduct item = productMapper.getProductByNum(pro_number);
        ScomCategory category = categoryMapper.getCategoryById(item.getCid());
        ScomShop shop = shopMapper.getShopById(item.getShop_id());
        item.setCid_name(category.getName());
        item.setShop_name(shop.getName());
        return ResponseDataUtil.success("获取产品成功", item);
    }

    @Override
    public ResponseData saveProduct(ScomProduct product){
        // product.setPrice_jf((int)Math.floor(product.getPrice()));
        product.setPrice_jf(0);
        product.setCreate_time(new Date().getTime());
        product.setUpdate_time(new Date().getTime());
        String uuid = UUID.randomUUID().toString().replace("-", "");
        product.setPro_number(uuid);
        product.setSale_num(0);
        Integer num = productMapper.saveProduct(product);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存产品失败", null);
        }
        return ResponseDataUtil.success("保存产品成功", null);
    }

    @Override
    public ResponseData updateProduct(ScomProduct product){
        product.setUpdate_time(new Date().getTime());
        Integer num = productMapper.updateProduct(product);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改产品失败", null);
        }
        return ResponseDataUtil.success("修改产品成功", null);
    }

    @Override
    public ResponseData deleteProduct(Long id){
        Integer num = productMapper.deleteProduct(id);
        return ResponseDataUtil.success("删除产品成功", null);
    }
}
